﻿@using Entidades.Sistema.Venta
@{
    Html.EnableClientValidation();
    Html.EnableUnobtrusiveJavaScript();

    var grid = Html.DevExpress().GridView(
        settings =>
        {
            settings.Name = "gvEditing";
            settings.KeyFieldName = "IdProducto";
            settings.CallbackRouteValues = new { Controller = "Productos", Action = "EditProductosPartial" };
            settings.SettingsEditing.AddNewRowRouteValues = new { Controller = "Productos", Action = "EditProductosAddNewPartial" };
            settings.SettingsEditing.UpdateRowRouteValues = new { Controller = "Productos", Action = "EditProductosUpdatePartial" };
            settings.SettingsEditing.DeleteRowRouteValues = new { Controller = "Productos", Action = "EditProductosDeletePartial" };
            settings.SettingsEditing.Mode = GridViewEditingMode.PopupEditForm;
            
            settings.SettingsBehavior.ConfirmDelete = true;
            settings.SettingsPopup.EditForm.Width = 800;
            settings.SettingsPopup.EditForm.HorizontalAlign = PopupHorizontalAlign.Center;
            settings.SettingsPopup.EditForm.VerticalAlign = PopupVerticalAlign.WindowCenter;
            
            settings.SettingsPager.FirstPageButton.Visible = true;
            settings.SettingsPager.LastPageButton.Visible = true;
            settings.SettingsPager.PageSizeItemSettings.Visible = true;
            settings.SettingsPager.PageSizeItemSettings.Items = new string[] { "10", "20", "50" };
            
            
            settings.Settings.ShowFilterRow = true;
            settings.Settings.ShowFilterRowMenu = true;
            settings.CommandColumn.Visible = true;
            settings.CommandColumn.ShowClearFilterButton = true;
            
            settings.Width = System.Web.UI.WebControls.Unit.Percentage(100);

            settings.CommandColumn.Visible = true;
            settings.CommandColumn.ShowNewButtonInHeader = ViewBag.PermisoCreate;
            settings.CommandColumn.ShowEditButton = ViewBag.PermisoEdit;
            settings.CommandColumn.ShowDeleteButton = ViewBag.PermisoDelete;

            settings.Columns.Add("Nombre");
            settings.Columns.Add("Descripcion");
            settings.Columns.Add(column =>
            {
                column.FieldName = "TipoProducto.IdTipoProducto";
                column.Caption = "Tipo Producto";

                column.ColumnType = MVCxGridViewColumnType.ComboBox;
                var comboBoxProperties = column.PropertiesEdit as ComboBoxProperties;                
                comboBoxProperties.DataSource = ViewBag.TipoProductos;
                comboBoxProperties.TextField = "Nombre";
                comboBoxProperties.ValueField = "IdTipoProducto";
                comboBoxProperties.ValidationSettings.ErrorDisplayMode = ErrorDisplayMode.Text;
                comboBoxProperties.IncrementalFilteringMode = IncrementalFilteringMode.StartsWith;
                comboBoxProperties.DropDownStyle = DropDownStyle.DropDown;
                
                column.SetEditItemTemplateContent(container =>
                {
                    Html.DevExpress().ComboBox(editorSettings =>
                    {
                        editorSettings.Name = "IdTipoProducto";
                        editorSettings.Properties.TextField = "Nombre";
                        editorSettings.Properties.ValueField = "IdTipoProducto";
                        editorSettings.Properties.Width = 120;
                        editorSettings.Properties.ValueType = typeof(int);
                        editorSettings.Properties.ValidationSettings.EnableCustomValidation = true;
                        editorSettings.ShowModelErrors = true;
                        editorSettings.Properties.ValidationSettings.ErrorDisplayMode = ErrorDisplayMode.ImageWithTooltip;
                        editorSettings.Properties.IncrementalFilteringMode = IncrementalFilteringMode.StartsWith;

                    }).BindList(ViewBag.TipoProductos).Bind(DataBinder.Eval(container.DataItem, "TipoProducto.IdTipoProducto")).Render();
                });
            });



            settings.Columns.Add(column =>
            {
                column.FieldName = "Unidad.IdUnidadMedida";
                column.Caption = "Unidad";

                column.ColumnType = MVCxGridViewColumnType.ComboBox;
                var comboBoxProperties = column.PropertiesEdit as ComboBoxProperties;
                comboBoxProperties.DataSource = ViewBag.Unidad;
                comboBoxProperties.TextField = "Nombre";
                comboBoxProperties.ValueField = "IdUnidadMedida";
                comboBoxProperties.ValidationSettings.ErrorDisplayMode = ErrorDisplayMode.Text;
                comboBoxProperties.IncrementalFilteringMode = IncrementalFilteringMode.StartsWith;
                comboBoxProperties.DropDownStyle = DropDownStyle.DropDown;

                column.SetEditItemTemplateContent(container =>
                {
                    Html.DevExpress().ComboBox(editorSettings =>
                    {
                        editorSettings.Name = "IdUnidadMedida";
                        editorSettings.Properties.TextField = "Nombre";
                        editorSettings.Properties.ValueField = "IdUnidadMedida";
                        editorSettings.Properties.Width = 120;
                        editorSettings.Properties.ValueType = typeof(int);
                        editorSettings.Properties.ValidationSettings.EnableCustomValidation = true;
                        editorSettings.Properties.ValidationSettings.ErrorDisplayMode = ErrorDisplayMode.ImageWithText;
                        editorSettings.ShowModelErrors = true;
                        editorSettings.Properties.ValidationSettings.ErrorDisplayMode = ErrorDisplayMode.ImageWithTooltip;
                        editorSettings.Properties.IncrementalFilteringMode = IncrementalFilteringMode.StartsWith;

                    }).BindList(ViewBag.Unidad).Bind(DataBinder.Eval(container.DataItem, "Unidad.IdUnidadMedida")).Render();
                });
            });

            //settings.Columns.Add(
            //    column =>
            //    {
            //        column.FieldName = "Unidad.IdUnidadMedida";
            //        column.Caption = "Unidad";
            //        column.Width = 80;
            //        column.ColumnType = MVCxGridViewColumnType.ComboBox;
            //        var comboBoxProperties = column.PropertiesEdit as ComboBoxProperties;
            //        comboBoxProperties.DataSource = ViewBag.Unidad;
            //        comboBoxProperties.TextField = "Nombre";
            //        comboBoxProperties.ValueField = "IdUnidadMedida";
            //        comboBoxProperties.ValueType = typeof(int);
            //        comboBoxProperties.DropDownStyle = DropDownStyle.DropDown;
            //    });    
            
            
            settings.Columns.Add(column =>
            {
                column.FieldName = "CantidadPorUnidadProd";
                column.Caption = "Cant. x Unid.";
                column.Width = 80;
                                
                column.ColumnType = MVCxGridViewColumnType.SpinEdit;
                var spinEditProperties = column.PropertiesEdit as SpinEditProperties;
                spinEditProperties.DisplayFormatInEditMode = true;
                spinEditProperties.MinValue = 0;
                spinEditProperties.MaxValue = 1000000;
            });


            settings.Columns.Add(
                column =>
                {
                    column.FieldName = "Alicuota";
                    column.Caption = "Alicuota";
                    column.Width = 80;
                    column.ColumnType = MVCxGridViewColumnType.ComboBox;
                    var comboBoxProperties = column.PropertiesEdit as ComboBoxProperties;
                    comboBoxProperties.DataSource = ViewBag.Alicuota;
                    comboBoxProperties.DropDownStyle = DropDownStyle.DropDown;
                });
            
            settings.Columns.Add(column =>
            {
                column.FieldName = "CantidadPorTurno";
                column.Caption = "Cant. x Turno.";
                column.Width = 80;
                column.ColumnType = MVCxGridViewColumnType.SpinEdit;
                var spinEditProperties = column.PropertiesEdit as SpinEditProperties;
                spinEditProperties.DisplayFormatInEditMode = true;
                spinEditProperties.MinValue = 0;
                spinEditProperties.MaxValue = 1000000;
            });
            
            settings.Columns.Add(column =>
            {
                column.FieldName = "DiasVencimiento";

                column.ColumnType = MVCxGridViewColumnType.SpinEdit;
                column.Width = 100;
                var spinEditProperties = column.PropertiesEdit as SpinEditProperties;
                spinEditProperties.DisplayFormatInEditMode = true;
                spinEditProperties.MinValue = 0;
                spinEditProperties.MaxValue = 1000000;
            });
            
            settings.Columns.Add(column => {
                column.FieldName = "PrecioLista";                

                column.ColumnType = MVCxGridViewColumnType.SpinEdit;
                column.Width = 100;
                var spinEditProperties = column.PropertiesEdit as SpinEditProperties;
                spinEditProperties.DisplayFormatString = "c";
                spinEditProperties.DisplayFormatInEditMode = true;
                spinEditProperties.MinValue = 0;
                spinEditProperties.MaxValue = 1000000;
            });

            settings.Columns.Add(column =>
            {
                column.FieldName = "PesoNeto";
                column.Caption = "Peso Neto(gramos)";

                column.ColumnType = MVCxGridViewColumnType.SpinEdit;
                column.Width = 100;
                var spinEditProperties = column.PropertiesEdit as SpinEditProperties;
                spinEditProperties.DisplayFormatInEditMode = true;
                spinEditProperties.MinValue = 0;
                spinEditProperties.MaxValue = 1000000;
            });

            settings.PreRender = (s, e) =>
            {
                var sender = (MVCxGridView)s;
                sender.FocusedRowIndex = 1;
            };

            settings.SettingsBehavior.AllowFocusedRow = true;
           
        });

    if (ViewData["EditError"] != null){
        grid.SetEditErrorText((string)ViewData["EditError"]);
    }
}

@grid.Bind(Model).GetHtml()




